//
//  UIImageCategory.m
//  MokoAPP
//
//  Created by 현식 강 on 11. 8. 7..
//  Copyright 2011 아이온. All rights reserved.
//

#import "UIImageCategory.h"


@implementation UIImage (UIImageCategory)
-(UIImage*) ResizedImage:(CGRect)rect {
    CGFloat ratio = 0.0f;
    if( self.size.width > self.size.height ) {
        ratio = rect.size.width / self.size.width;
    } else {
        ratio = rect.size.width / self.size.height;
    }
    CGRect newrect = CGRectMake(0, 0, ratio*self.size.width, ratio*self.size.height);
    if( UIGraphicsBeginImageContextWithOptions != NULL ) {
        UIGraphicsBeginImageContextWithOptions(newrect.size, NO, 0.0f);
    } else {
        UIGraphicsBeginImageContext(newrect.size);
    }
    [self drawInRect:rect];
    return UIGraphicsGetImageFromCurrentImageContext();
}
-(UIImage*) ResizedLimitedWidth:(CGFloat)width height:(CGFloat)height {
    CGSize newSize = CGSizeMake(0, 0);
    if( width > 0 && self.size.width > width ) {
        newSize.height = (self.size.height * width) / self.size.width;
        newSize.width = width;
    }
    else if( height > 0 && self.size.height > height ) {
        newSize.width = (self.size.width * height) / self.size.height;
        newSize.height = height;
    }
    
    CGRect newrect = CGRectMake(0, 0, newSize.width, newSize.height);
    if( UIGraphicsBeginImageContextWithOptions != NULL ) {
        UIGraphicsBeginImageContextWithOptions(newrect.size, NO, 0.0f);
    } else {
        UIGraphicsBeginImageContext(newrect.size);
    }
    [self drawInRect:newrect];
    
    return UIGraphicsGetImageFromCurrentImageContext();
}
@end
